﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.SysInterface;
using RoadTeam.Pub;
using RoadTeam.Enums;

namespace RoadTeam.BLL
{
    public class RmUnit2Bll
    {
        private static object o = new object();
        public DataTable GetUnitData(String OrganIdentifier, int CurrentPage, int PageSize)
        {
            String OrganId = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            int AllPageCount = FactoryClass.CreateRmUnit().GetUnitDataAllPageCount(OrganId);
            lock (o)
            {
                DataTable dt = FactoryClass.CreateRmUnit().GetUnitDataByPage(OrganId, PageSize, CurrentPage, AllPageCount);
                return SyncUnitData(dt);
            }
        }

        private DataTable GetUnitTable(string StrUnitIdentifiers)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = @"  UnitIdentifier IN (" + StrUnitIdentifiers + ") ";
            return RmUnit2.Select(where, GlobalConfig.ConnectionString);
        }

        private void InsertUnitInfoSync(DataTable Rt_Unit, DataTable dt_Unit)
        {
            DataRow dr_Unit;
            String UnitIdentifier = @"UnitIdentifier";
            foreach (DataRow Rt_Dr in Rt_Unit.Rows)
            {
                if (Rt_Dr["UnitIdentifier"] != null && !String.IsNullOrEmpty(Rt_Dr["UnitIdentifier"].ToString())) continue;
                dr_Unit = dt_Unit.NewRow();
                foreach (DataColumn Rt_Dc in Rt_Unit.Columns)
                {
                    foreach (DataColumn Dc in dt_Unit.Columns)
                    {
                        if (Rt_Dc.ColumnName.ToLower() == Dc.ColumnName.ToLower())
                        {
                            if (Rt_Dc.ColumnName.ToLower() != UnitIdentifier.ToLower() && Dc.ColumnName.ToLower() != UnitIdentifier.ToLower())
                            {
                                dr_Unit[Dc.ColumnName.ToLower()] = Rt_Dr[Rt_Dc.ColumnName.ToLower()];
                            }
                            else
                            {
                                String Identifier = CreateIdentifier.GetIdentifier();
                                dr_Unit[Dc.ColumnName.ToLower()] = Identifier;
                                Rt_Dr[Rt_Dc.ColumnName.ToLower()] = Identifier;
                            }
                        }
                    }
                }
                dt_Unit.Rows.Add(dr_Unit);
            }
        }


        public DataTable SyncUnitData(DataTable dt)
        {
            if (dt.Rows.Count == 0) return new DataTable();

            SyncBuildingPavilionData(dt);
            String StrUnitIdentifiers = @"'-1'";
            #region 不可用的方法
            //List<String> liUnitIdentifier = new List<string>();
            //foreach (DataRow dr in dt.Rows)
            //{
            //    if (dr["UnitIdentifier"] != null && !String.IsNullOrEmpty(dr["UnitIdentifier"].ToString()))
            //    {
            //        StrUnitIdentifiers += ",'" + dr["UnitIdentifier"].ToString() + "'";
            //        liUnitIdentifier.Add(dr["UnitIdentifier"].ToString());
            //    }
            //}

            //DataRow[] drs;
            //drs = dt.Select(@" UnitIdentifier IS NULL ");
            //if (drs.Count() != 0)
            //{
            //    //InsertUnitInfoSync(ToDataTable(drs), dt_Unit);
            //    InsertUnitInfoSync(dt, dt_Unit);
            //}
            ////根据Bs系统中的分页数据查找Server数据库中[RmUnit2]中的数据
            ////更新Server数据库[RmUnit2]中的已经存在的数据
            ////在Server数据库中[RmUnit2]表中没有查找到的,新建列插入Server数据库中
            //if (liUnitIdentifier.Count > 0)//如果已经存在在Server数据库,查找出来并且更新
            //{
            //    drs = dt.Select(@" UnitIdentifier IN (" + StrUnitIdentifiers + ")");
            //    UpdateUnitInfoSync(ConverTable.ToDataTable(drs), dt_Unit);
            //}

            #endregion
            DataTable dt_Unit = GetUnitTable(StrUnitIdentifiers);
            DataRow dr_Unit;
            foreach (DataRow dr in dt.Rows)
            {
                dr_Unit = GetUnitDateRow(dr["UnitIdentifier"].ToString());

                if (dr_Unit != null)
                {
                    SyncRtUnitToUnit(dr, dr_Unit);
                    dt_Unit.ImportRow(dr_Unit);
                }
                else
                {
                    //新增算法
                    dr_Unit = dt_Unit.NewRow();
                    SyncRtUnitToUnit(dr, dr_Unit);
                    dt_Unit.Rows.Add(dr_Unit);
                }

            }
            RmUnit2.InsertDatatable(dt_Unit, GlobalConfig.ConnectionString);
            RmUnit2.Update(dt_Unit, GlobalConfig.ConnectionString);
            return dt_Unit;
        }

        public DataRow GetUnitDateRow(String UnitIdentifier)
        {
            SqlWhere where = new SqlWhere();
            where.Info = new RmUnit2() { UnitIdentifier = UnitIdentifier };
            DataTable dt = RmUnit2.Select(where, GlobalConfig.ConnectionString);
            if (dt.Rows.Count == 0) return null;
            return dt.Rows[0];
        }




        private void SyncRtUnitToUnit(DataRow Rt_Prodr, DataRow dr_Pro)
        {
            foreach (DataColumn Rt_Dc in Rt_Prodr.Table.Columns)
            {
                foreach (DataColumn Dc in dr_Pro.Table.Columns)
                {
                    if (Rt_Dc.ColumnName.ToLower() == Dc.ColumnName.ToLower())
                    {
                        dr_Pro[Dc.ColumnName.ToLower()] = Rt_Prodr[Rt_Dc.ColumnName.ToLower()];
                    }
                }
            }
        }



        private void UpdateUnitInfoSync(DataTable Rt_Unit, DataTable dt_Unit)
        {
            #region 同步表中单元信息
            String Rtidentity = @"UnitIdentifier";
            String idtntity = @"UnitIdentifier";
            String EnmunType = @"UnitColumnDictionary2";
            RoadTeam.Pub.SyncTable.SyncDataTable(Rt_Unit, dt_Unit, Rtidentity, idtntity, EnmunType);
            RmUnit2.Update(dt_Unit, GlobalConfig.ConnectionString);
            #endregion
        }



        public void SyncBuildingPavilionData(DataTable Rt_Unit)
        {
            #region 同步表中楼阁,楼盘信息
            #region 更新楼阁标识
            List<string> li = new List<string>();
            String StrIn = String.Empty;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                if (dr["PavilionID"] != null && !li.Contains(dr["PavilionID"].ToString()))
                {
                    li.Add(dr["PavilionID"].ToString());
                    StrIn += "'" + dr["PavilionID"].ToString() + "',";
                }
            }

            DataTable dt_Pavilion = FactoryClass.CreateRmPavilion().GetPavilionData(StrIn.TrimEnd(','));
            DataRow dr_Pavilion;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                dr_Pavilion = dt_Pavilion.Select(@"PavilionID=" + dr["PavilionID"].ToString())[0];
                if (dr_Pavilion["PavilionIdentifier"] == null ||
                    String.IsNullOrEmpty(dr_Pavilion["PavilionIdentifier"].ToString()))
                {
                    dr_Pavilion["PavilionIdentifier"] = CreateIdentifier.GetIdentifier();
                    dr["PavilionIdentifier"] = dr_Pavilion["PavilionIdentifier"];
                }
                else
                {
                    dr["PavilionIdentifier"] = dr_Pavilion["PavilionIdentifier"];
                }
            }
            FactoryClass.CreateRmPavilion().UpdatePavilionData(dt_Pavilion);
            #endregion
            #region 更新楼盘标识
            li = new List<string>();
            StrIn = String.Empty;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                if (dr["BuildingID"] != null && !li.Contains(dr["BuildingID"].ToString()))
                {
                    li.Add(dr["BuildingID"].ToString());
                    StrIn += "'" + dr["BuildingID"].ToString() + "',";
                }
            }
            DataTable dt_Building = FactoryClass.CreateRmBuilding().GetBuildingData(StrIn.TrimEnd(','));
            DataRow dr_Building;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                dr_Building = dt_Building.Select(@"BuildingID=" + dr["BuildingID"].ToString())[0];
                if (dr_Building["BuildingIdentifier"] == null || String.IsNullOrEmpty(dr_Building["BuildingIdentifier"].ToString()))
                {
                    dr_Building["BuildingIdentifier"] = CreateIdentifier.GetIdentifier();
                    dr["BuildingIdentifier"] = dr_Building["BuildingIdentifier"];
                }
                else
                {
                    dr["BuildingIdentifier"] = dr_Building["BuildingIdentifier"];
                }
            }
            FactoryClass.CreateRmBuilding().UpdateBuildingData(dt_Building);
            #endregion

            li = new List<string>();
            StrIn = String.Empty;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                if (dr["UnitID"] != null && !li.Contains(dr["UnitID"].ToString()))
                {
                    li.Add(dr["UnitID"].ToString());
                    StrIn += "'" + dr["UnitID"].ToString() + "',";
                }
            }

            DataTable dt_Unit = FactoryClass.CreateRmUnit().GetUnitData(StrIn.TrimEnd(','));
            DataRow dr_Unit;
            foreach (DataRow dr in Rt_Unit.Rows)
            {
                dr_Unit = dt_Unit.Select(@"UnitID=" + dr["UnitID"].ToString())[0];
                if (dr_Unit["UnitIdentifier"] == null ||
                    String.IsNullOrEmpty(dr_Unit["UnitIdentifier"].ToString()))
                {
                    dr_Unit["UnitIdentifier"] = CreateIdentifier.GetIdentifier();
                    dr["UnitIdentifier"] = dr_Unit["UnitIdentifier"];
                }
                else
                {
                    dr["UnitIdentifier"] = dr_Unit["UnitIdentifier"];
                }
            }
            FactoryClass.CreateRmUnit().UpdateUnitTable(dt_Unit);
            //TbRmUnit.Update(Rt_Unit, GlobalConfig.RtConnectionString);
            #endregion
        }

        public int GetUnitAllPageCount(String OrganIdentifier)
        {
            String OrganId = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            int AllPageCount = FactoryClass.CreateRmUnit().GetUnitDataAllPageCount(OrganId);
            return AllPageCount;
        }
    }
}
